﻿using NPinyin;
using SqlSugar;
using System.Drawing;
using System.Security.Cryptography;
using Wq.Extensions.Common;
using Wq.Extensions.Repository;
using Wq.ManagerService;
using Wq.MeetingService;
using Wq.MeetingService.Entities;
using Wq.MeetingService.Models;
using Wq.MeetingService.Models.Enum;
using Wq.Model;
namespace Wq.Application
{
    /// <summary>
    /// 会场
    /// </summary>
    public class MeetingApplication : BaseApplication
    {
        private readonly VenueService venueService;
        private readonly MeetingRoomService meetingRoomService;
        private readonly GuestRoomServicee guestRoomServicee;
        private readonly DiningRommService diningRommService;
        private readonly VenuFeaturesService venuFeaturesService;
        private readonly FeaturesService featuresService;
        private readonly MeetingService.CategoryService categoryService;
        private readonly RedisBaseRepository redisRepository;
        private readonly FacilitieService facilitieService;
        private readonly VenueFacilitieService venueFacilitieService;
        private readonly BrandService brandService;
        private readonly MeetingThumbnailService meetingThumbnailService;
        private readonly PrivateRoomService privateRoomService;
        private readonly RoomLayoutService roomLayoutService;
        private readonly RegionService regionService;
        private readonly LogService logService;
        /// <summary>
        /// 会场
        /// </summary>
        public MeetingApplication(VenueService venueService, 
            MeetingRoomService meetingRoomService, 
            GuestRoomServicee guestRoomServicee, 
            DiningRommService diningRommService,
            VenuFeaturesService venuFeaturesService,
            FeaturesService featuresService,
            MeetingService.CategoryService categoryService,
            RedisBaseRepository redisRepository,
            FacilitieService facilitieService,
            VenueFacilitieService venueFacilitieService,
            BrandService brandService,
            MeetingThumbnailService meetingThumbnailService,
            PrivateRoomService privateRoomService,
            RoomLayoutService roomLayoutService,
            RegionService regionService,
            LogService logService
            )
        {
            this.venueService = venueService;
            this.meetingRoomService = meetingRoomService;
            this.guestRoomServicee = guestRoomServicee;
            this.diningRommService = diningRommService;
            this.venuFeaturesService = venuFeaturesService;
            this.featuresService = featuresService;
            this.categoryService = categoryService;
            this.redisRepository = redisRepository;
            this.facilitieService = facilitieService;
            this.venueFacilitieService = venueFacilitieService;
            this.brandService = brandService;
            this.meetingThumbnailService = meetingThumbnailService;
            this.privateRoomService = privateRoomService;
            this.roomLayoutService = roomLayoutService;
            this.regionService = regionService;
            this.logService = logService;
        }
        #region 会场

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="editor"></param>
        private void Verify(VenueEdit model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
                throw new MyException("场地名称不能为空");
            if (model.CategoryId == 0)
                throw new MyException("请选择分类");
            if (model.RegionId == 0)
                throw new MyException("请选择地区");
            if (string.IsNullOrWhiteSpace(model.Address))
                throw new MyException("详细地址不能为空");
            if (string.IsNullOrWhiteSpace(model.Description))
                throw new MyException("描述不能为空");
            if (model.Longitude == 0)
                throw new MyException("请选择地址");
            if (model.Images.Count == 0)
                throw new MyException("请上传图片");
        }

        /// <summary>
        /// 添加场地
        /// </summary>
        /// <param name="model"></param>
        public void Insert(VenueEdit info)
        {
            Verify(info);
            info.RegionFullName = regionService.GetFullName(info.RegionId);
            info.RegionFullIds = regionService.GetFullIds(info.RegionId, ",");
            venueService.Create(info);
            Log(null, info);
        }

        /// <summary>
        /// 修改场地
        /// </summary>
        /// <param name="model"></param>
        public  void Update(VenueEdit info)
        {
            var old = GetVenueEditor(info.Id);
            Verify(info);
            venueService.Save(info);
            Log(old, info);
            redisRepository.Remove(SysCacheKey.EnueModel(info.Id));
        }

        public VenueEdit GetVenueEditor(long id)
        {
            var model = VenueDetails(id);
            var info = model.Map<VenueEdit>();
            info.Images = model.Thumbnails.GroupBy(p => p.TypeId).ToDictionary(p => p.Key, p => p.ToList().Select(p => p.FilePath).ToList());
            info.Configs = model.Facilities.GroupBy(p => p.TypeId).ToDictionary(p => p.Key, p => p.ToList().Select(p => p.FacilitieId).ToList());
            info.VenueFeatures = model.Features.Select(p => p.FeaturesId).ToList();
            return info;
        }

        /// <summary>
        /// 获取会场实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public VenueInfo VenueDetails(long id) => 
            redisRepository.Get(SysCacheKey.EnueModel(id), () =>
        {
            var info = venueService.GetById(id);
            var meetings = meetingRoomService.GetList(p => p.VenueId == id);
            var dinings = diningRommService.GetList(p => p.VenueId == id);
            var guests = guestRoomServicee.GetList(p => p.VenueId == id);
            var thumbnails = meetingThumbnailService.GetList(p => p.VenueId == id);
            var facilities = venueFacilitieService.GetList(p => p.VenueId == id);
            var features = venuFeaturesService.GetList(p => p.VenueId == id);
            var layout = roomLayoutService.GetList(p => p.VenueId == id);
            var privates = privateRoomService.GetList(p => p.VenueId == id);
            if (meetings?.Count > 0)
            {
                meetings.ForEach(item =>
                {
                    item.Layouts = layout.FindAll(p => p.MeetingRoomId == item.Id);
                    item.Facilities = facilities.FindAll(p => p.MeetingRoomId == item.Id);
                    item.Thumbnails = thumbnails.FindAll(p => p.SourceRoomId == item.Id);
                    item.Features = features.FindAll(p => p.MeetingRoomId == item.Id);
                });
                info.Meetings = meetings;
            }
            if (dinings?.Count > 0)
            {
                dinings.ForEach(item =>
                {
                    item.Thumbnails = thumbnails.FindAll(p => p.SourceRoomId == item.Id);
                    item.PrivateRooms = privates.FindAll(p => p.DiningRommId == item.Id);
                });
                info.Dinings = dinings;
            }
            if (guests?.Count > 0)
            {
                guests.ForEach(item => {
                    item.Thumbnails = thumbnails.FindAll(p => p.SourceRoomId == item.Id);
                });
                info.Guests = guests;
            }
            info.Thumbnails = thumbnails.FindAll(p => p.SourceRoomId == 0);
            info.Facilities = facilities.FindAll(p => p.MeetingRoomId == 0);
            info.Features = features.FindAll(p => p.MeetingRoomId == 0);
            return info;

        }).Result;
        

        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="oldTag"></param>
        /// <param name="newTag"></param>
        /// <param name="isDelete"></param>
        public void Log(VenueEdit oldSite, VenueEdit newSite = null, bool isDelete = false)
        {
            string msg = string.Empty;
            if (isDelete)
                msg = $"删除场地【{oldSite.Name}】";
            else
            {

                if (newSite.Id == 0)
                    msg = $"新增场地【{newSite.Name}】";
                else
                {
                    msg = $"修改场地【{newSite.Name}】";
                    if (newSite.Name != oldSite.Name)
                        msg += $" 名称由【{oldSite.Name}】变为【{newSite.Name}】";
                    if (newSite.CategoryId != oldSite.CategoryId)
                        msg += $" 分类由【{categoryService.GetById(oldSite.CategoryId).Name}】变为【{categoryService.GetById(newSite.CategoryId).Name}】";
                    if (newSite.RegionId != oldSite.RegionId)
                        msg += $" 地址由【{regionService.GetFullName(oldSite.RegionId)}】变为【{regionService.GetFullName(newSite.RegionId)}】";
                    if (newSite.Description != oldSite.Description)
                        msg += $" 详细地址由【{oldSite.Description}】变为【{newSite.Description}】";
                }
            }

            logService.Create(LogModule.Site, msg);

        }

        /// <summary>
        /// 删除场地
        /// </summary>
        /// <param name="id"></param>
        public async void VenueDelete(long id)
        {
            venueService.DeleteById(id);
            meetingRoomService.Delete(p => p.VenueId == id);
            guestRoomServicee.Delete(p => p.VenueId == id);
            diningRommService.Delete(p => p.VenueId == id);
            meetingThumbnailService.Delete(p => p.VenueId == id);
            privateRoomService.Delete(p => p.VenueId == id);
            venuFeaturesService.Delete(p => p.VenueId == id);
            roomLayoutService.Delete(p => p.VenueId == id);

            var old = GetVenueEditor(id);
            Log(old, isDelete: true);
            await redisRepository.Remove(SysCacheKey.EnueModel(id));
        }

        /// <summary>
        /// 场地列表
        /// </summary>
        /// <param name="pageModel"></param>
        /// <returns></returns>
        public MessageModel Venues(PageModel<VenueQuery> pageModel) => new MessageModel { Data = venueService.List(pageModel),Count = pageModel.Page.Num };

        /// <summary>
        /// 修改场地排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public void UpdatetVenuesSequence(long id, long sequence)
        {
            venueService.UpdateSequence(id, sequence);
            redisRepository.Remove(SysCacheKey.EnueModel(id));
        }
   

        /// <summary>
        /// 获取前top个场地
        /// </summary>
        /// <param name="regionId"></param>
        /// <param name="businessId"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<VenueBaseView> ListByCity(long regionId, long businessId, int top) => venueService.ListByCity(regionId, businessId, top);
        #endregion

        #region 会议室


        /// <summary>
        /// 添加会议室
        /// </summary>
        /// <param name="model"></param>
        public  void Insert(MeetingRoomEdit model)
        {
          
            meetingRoomService.Create(model);
            MeetingLog(newMeeting: model);
             redisRepository.Remove(SysCacheKey.EnueModel(model.VenueId));
        }

        /// <summary>
        /// 编辑会议室
        /// </summary>
        /// <param name="model"></param>
        public  void Update(MeetingRoomEdit model)
        {
            var oldMeeting = MeetingEditor(model.Id);
             meetingRoomService.Save(model);
            MeetingLog(oldMeeting, model);
             redisRepository.Remove(SysCacheKey.EnueModel(model.VenueId));
        }

        /// <summary>
        /// 删除会议室
        /// </summary>
        /// <param name="id"></param>
        public async void MeetingDelete(int id)
        {
            var old = MeetingEditor(id);
            var model = meetingRoomService.GetById(id);
            meetingRoomService.DeleteById(id);
            roomLayoutService.Delete(p => p.MeetingRoomId == id);
            meetingThumbnailService.Delete(p => p.SourceRoomId  == id);
            venuFeaturesService.Delete(p => p.MeetingRoomId == id);
            meetingRoomService.UpdateVenue(model.VenueId);
            MeetingLog(oldMeeting: old, isDelete: true);
            await redisRepository.Remove(SysCacheKey.EnueModel(model.VenueId));
        }

        /// <summary>
        /// 会议室日志 
        /// </summary>
        public void MeetingLog(MeetingRoomEdit oldMeeting = null, MeetingRoomEdit newMeeting = null, bool isDelete = false)
        {
            string msg = string.Empty;
            if (isDelete)
                msg = $"删除会议室【{oldMeeting.Name}】";
            else
            {
                if (newMeeting.Id == 0)
                    msg = $"新增会议室【{newMeeting.Name}】";
                else
                {
                    msg = $"修改会议室【{newMeeting.Name}】";
                    if (newMeeting.Name != oldMeeting.Name)
                        msg += $" 名称由【{oldMeeting.Name}】变为【{newMeeting.Name}】";
                }
            }
            logService.Create(LogModule.MeetingRoom, msg);
        }

        /// <summary>
        /// 会议室列表
        /// </summary>
        /// <param name="venueId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<MeetingRoomView> Meetings(long venueId, string name)
        {
            var exp = Expressionable.Create<MeetingRoomInfo>().And(p => p.VenueId == venueId).AndIF(!string.IsNullOrWhiteSpace(name), p => p.Name.Contains(name)).ToExpression();
            return meetingRoomService.GetList(exp).Map<MeetingRoomView>();
        }

        /// <summary>
        ///  会议室详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MeetingRoomEdit MeetingEditor(long id)
        {
          var model = meetingRoomService.Details(id);
          var info = model.Map<MeetingRoomEdit>();
            info.Images = model.Thumbnails.Select(p => p.FilePath).ToList();
            info.FacilitieIds = model.Facilities.Select(p => p.FacilitieId).ToList();
            info.FeaturesIds = model.Features.Select(p => p.FeaturesId).ToList();
            return info;
        }
        /// <summary>
        /// 修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public void UpdateMeetingSequence(long id, long sequence) => meetingRoomService.UpdateSequence(id, sequence);

        #endregion

        #region 客房

        /// <summary>
        /// 保存客房
        /// </summary>
        /// <param name="model"></param>
        public void Insert(GuestRoomEdit model) {
            guestRoomServicee.Create(model);
            GuestLog(newMeeting: model);
            redisRepository.Remove(SysCacheKey.EnueModel(model.VenueId));
        }


        /// <summary>
        /// 保存客房
        /// </summary>
        /// <param name="model"></param>
        public void Update(GuestRoomEdit model)
        {
            var old = GuestEditor(model.Id);
            guestRoomServicee.Save(model);
            GuestLog(old,model);
            redisRepository.Remove(SysCacheKey.EnueModel(model.VenueId));
        }

        /// <summary>
        /// 删除客房
        /// </summary>
        /// <param name="id"></param>
        public void GuestRoomDelete(long id)
        {
            var old = GuestEditor(id);
            guestRoomServicee.DeleteById(id);
            meetingThumbnailService.Delete(p => p.SourceRoomId == id);
            guestRoomServicee.UpdateVenue(old.VenueId);
            GuestLog(old, isDelete:true);
            redisRepository.Remove(SysCacheKey.EnueModel(old.VenueId));
        }

        /// <summary>
        /// 客房列表
        /// </summary>
        /// <param name="venueId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<GuestRoomInfo> Guests(long venueId, string name)
        {
            var exp = Expressionable.Create<GuestRoomInfo>().And(p => p.VenueId == venueId).AndIF(!string.IsNullOrWhiteSpace(name), p => p.Name.Contains(name)).ToExpression();
            return guestRoomServicee.GetList(exp);
        }

        /// <summary>
        ///  客房详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public GuestRoomEdit GuestEditor(long id)
        {
            var model = guestRoomServicee.Details(id);
            var info = model.Map<GuestRoomEdit>();
            info.Images = model.Thumbnails.Select(p => p.FilePath).ToList();
            return info;
        }

        /// <summary>
        /// 客房修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public void UpdateGuestSequence(long id, long sequence)
        {
            guestRoomServicee.UpdateSequence(id, sequence);
            var model = guestRoomServicee.Details(id);
            redisRepository.Remove(SysCacheKey.EnueModel(model.VenueId));

        }


        /// <summary>
        /// 客房日志 
        /// </summary>
        public void GuestLog(GuestRoomEdit oldMeeting = null, GuestRoomEdit newMeeting = null, bool isDelete = false)
        {
            string msg = string.Empty;
            if (isDelete)
                msg = $"删除客房【{oldMeeting.Name}】";
            else
            {
                if (newMeeting.Id == 0)
                    msg = $"新增客房【{newMeeting.Name}】";
                else
                {
                    msg = $"修改客房【{newMeeting.Name}】";
                    if (newMeeting.Name != oldMeeting.Name)
                        msg += $" 名称由【{oldMeeting.Name}】变为【{newMeeting.Name}】";
                }
            }
            logService.Create(LogModule.GuestRoom, msg);
        }
        #endregion

        #region 餐厅

        /// <summary>
        /// 保存餐厅
        /// </summary>
        /// <param name="model"></param>
        public void Insert(DiningRommEdit model)
        {
            diningRommService.Create(model);
            DiningLog(newData: model);
            redisRepository.Remove(SysCacheKey.EnueModel(model.VenueId));
        }

        /// <summary>
        /// 修改餐厅
        /// </summary>
        /// <param name="model"></param>
        public void Update(DiningRommEdit model)
        {
            var old = DiningsEdit(model.Id);
            diningRommService.Save(model);
            DiningLog(oldData: old , newData: model);
            redisRepository.Remove(SysCacheKey.EnueModel(model.VenueId));

        }
        /// <summary>
        /// 删除餐厅
        /// </summary>
        /// <param name="id"></param>
        public void DiningDelete(long id)
        {
           var old = DiningsEdit(id);
            diningRommService.DeleteById(id);
            privateRoomService.Delete(p => p.DiningRommId == id);
            meetingThumbnailService.Delete(p => p.SourceRoomId == id);
            DiningLog(oldData: old,isDelete:true);
            redisRepository.Remove(SysCacheKey.EnueModel(old.VenueId));

        }

        /// <summary>
        /// 餐厅列表
        /// </summary>
        /// <param name="venueId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<DiningRommInfo> Dinings(long venueId, string name)
        {
            var exp = Expressionable.Create<DiningRommInfo>().And(p => p.VenueId == venueId).AndIF(!string.IsNullOrWhiteSpace(name), p => p.Name.Contains(name)).ToExpression();
            return diningRommService.GetList(exp);
        }

        /// <summary>
        /// 获取餐厅详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DiningRommEdit DiningsEdit(long id)
        {
            var model = diningRommService.Details(id);
            var info = model.Map<DiningRommEdit>();
            info.Images = model.Thumbnails.Select(p => p.FilePath).ToList();
            return info;
        }
        /// <summary>
        /// 餐厅修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public void UpdateDiningSequence(long id, long sequence)
        {
            diningRommService.UpdateSequence(id, sequence);
            var model = diningRommService.Details(id);
            redisRepository.Remove(SysCacheKey.EnueModel(model.VenueId));
        }

        /// <summary>
        /// 餐厅日志 
        /// </summary>
        public void DiningLog(DiningRommEdit oldData = null, DiningRommEdit newData = null, bool isDelete = false)
        {
            string msg = string.Empty;
            if (isDelete)
                msg = $"删除餐厅【{oldData.Name}】";
            else
            {
                if (newData.Id == 0)
                    msg = $"新增餐厅【{newData.Name}】";
                else
                {
                    msg = $"修改餐厅【{newData.Name}】";
                    if (newData.Name != oldData.Name)
                        msg += $" 名称由【{oldData.Name}】变为【{newData.Name}】";
                }
            }
            logService.Create(LogModule.Dining, msg);
        }

        #region 包间

        /// <summary>
        /// 添加包间
        /// </summary>
        /// <param name="model"></param>
        public void Insert(PrivateRoomInfo model)
        {
            privateRoomService.Verify(model);
            privateRoomService.Insert(model);
            var dining = diningRommService.Details(model.DiningRommId);
            dining.PrivateRoomCount = privateRoomService.GetList(p => p.DiningRommId == model.DiningRommId).Sum(p => p.Quantity);
            diningRommService.Update(dining);
            redisRepository.Remove(SysCacheKey.EnueModel(dining.VenueId));
        }
        
        /// <summary>
        /// 修改包间
        /// </summary>
        /// <param name="model"></param>
        public void Update(PrivateRoomInfo model)
        {
            privateRoomService.Verify(model);
            privateRoomService.Update(model);
        }

        /// <summary>
        /// 删除包间
        /// </summary>
        /// <param name="id"></param>
        public void PrivateDelete(long id)
        {
            var model = privateRoomService.GetById(id);
            privateRoomService.DeleteById(id);

            var dining = diningRommService.Details(model.DiningRommId);
            dining.PrivateRoomCount = privateRoomService.GetList(p => p.DiningRommId == model.DiningRommId).Sum(p => p.Quantity);
            diningRommService.Update(dining);
            redisRepository.Remove(SysCacheKey.EnueModel(dining.VenueId));

        }

        /// <summary>
        /// 包间列表
        /// </summary>
        /// <param name="venueId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<PrivateRoomInfo> PrivateRooms(long diningRommId, string name)
        {
            var exp = Expressionable.Create<PrivateRoomInfo>().And(p => p.DiningRommId == diningRommId).AndIF(!string.IsNullOrWhiteSpace(name), p => p.Name.Contains(name)).ToExpression();
            return privateRoomService.GetList(exp);
        }

        /// <summary>
        /// 包间修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public void UpdatePrivateSequence(long id, long sequence)
        {
            privateRoomService.UpdateSequence(id, sequence);
            var model = privateRoomService.GetById(id);
            redisRepository.Remove(SysCacheKey.EnueModel(model.VenueId));
        } 
        #endregion

        #endregion

        #region 分类
        private void Verify(CategoryInfo model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
                throw new MyException("分类名称不能为空");
            if(categoryService.Count(p => p.Name == model.Name && p.Id != model.Id) > 0)
                throw new MyException("分类名称已经存在");
        }

        /// <summary>
        ///  添加分类
        /// </summary>
        /// <param name="model"></param>
        public async void Insert(CategoryInfo model)
        {
            Verify(model);
            categoryService.Insert(model);
            await redisRepository.Remove(SysCacheKey.MeetingCategories);
        }

        /// <summary>
        /// 修改分类
        /// </summary>
        /// <param name="model"></param>
        public async void Update(CategoryInfo model)
        {
            Verify(model);
            categoryService.Update(model);
            await redisRepository.Remove(SysCacheKey.MeetingCategories);
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id"></param>
        /// <exception cref="MyException"></exception>
        public async void CategoryDelete(long id)
        {
            if(venueService.Count(p => p.CategoryId == id) > 0)
                throw new MyException("分类下存在会场，不能删除");
            categoryService.DeleteById(id);
           await redisRepository.Remove(SysCacheKey.MeetingCategories);
        }

        /// <summary>
        /// 分类列表
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<CategoryInfo> Categorys(string name)
        {
           return redisRepository.Get(SysCacheKey.MeetingCategories, () =>
            {
                var exp = Expressionable.Create<CategoryInfo>().AndIF(!string.IsNullOrWhiteSpace(name), p => p.Name.Contains(name)).ToExpression();
                return categoryService.GetList(exp).OrderByDescending(p => p.Sequence).ToList();
            }).Result;
        }

        /// <summary>
        /// 修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public  void UpdateCategorySequence(long id, long sequence)
        {
            categoryService.UpdateSequence(id, sequence);
             redisRepository.Remove(SysCacheKey.MeetingCategories);
        }

        #endregion

        #region 特色
        private void Verify(FeaturesInfo model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
                throw new MyException("特色名称不能为空");
            if (featuresService.Count(p => p.Name == model.Name && p.TypeId == model.TypeId && p.Id != model.Id) > 0)
                throw new MyException("特色名称已经存在");
        }

        /// <summary>
        /// 添加特色
        /// </summary>
        /// <param name="model"></param>
        public async void Insert(FeaturesInfo model)
        {
            Verify(model);
            featuresService.Insert(model);
           await redisRepository.Remove(SysCacheKey.MeetingFeatures);
        }
        /// <summary>
        /// 修改特色
        /// </summary>
        /// <param name="model"></param>
        public async void Update(FeaturesInfo model)
        {
            Verify(model);
            featuresService.Update(model);
           await  redisRepository.Remove(SysCacheKey.MeetingFeatures);
        }
        /// <summary>
        /// 删除特色
        /// </summary>
        /// <param name="id"></param>

        public async void FeaturesDelete(long id)
        {
            if (venuFeaturesService.Count(p => p.FeaturesId == id) > 0)
                throw new MyException("会议室下有该特色不能删除");
            featuresService.DeleteById(id);
           await redisRepository.Remove(SysCacheKey.MeetingFeatures);
        }

        /// <summary>
        /// 特色列表
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<FeaturesInfo> Features(string name, PositionType? typeId)
        {
            var list = redisRepository.Get(SysCacheKey.MeetingFeatures, () =>
            {
                var exp = Expressionable.Create<FeaturesInfo>().ToExpression();
                return featuresService.GetList(exp);
            }).Result;
            if (!string.IsNullOrEmpty(name))
                list = list.Where(p => p.Name.Contains(name)).ToList();
            if(typeId.HasValue)
                list = list.Where(p => p.TypeId == typeId).ToList();
            return list;
        }

        /// <summary>
        /// 修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public void UpdateFeatureSequence(long id, long sequence)
        {
            featuresService.UpdateSequence(id, sequence);
            redisRepository.Remove(SysCacheKey.MeetingFeatures);
        }

        #endregion

        #region 设备
        private void Verify(FacilitieInfo model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
                throw new MyException("设备名称不能为空");
            if (facilitieService.Count(p => p.Name == model.Name && p.TypeId == model.TypeId && p.Id != model.Id) > 0)
                throw new MyException("设备名称已经存在");
        }

        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="model"></param>
        public void Insert(FacilitieInfo model)
        {
            Verify(model);
            facilitieService.Insert(model);
        }
        /// <summary>
        /// 编辑设备
        /// </summary>
        /// <param name="model"></param>
        public void Update(FacilitieInfo model)
        {
            Verify(model);
            facilitieService.Update(model);
        }

        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="id"></param>
        /// <exception cref="MyException"></exception>
        public void FacilitieDelete(long id)
        {
            if (venueFacilitieService.Count(p => p.MeetingRoomId == id) > 0)
                throw new MyException("会议室下有该设备不能删除");

            if (venueFacilitieService.Count(p => p.FacilitieId == id) > 0)
                throw new MyException("会场下有该设备不能删除");
            facilitieService.DeleteById(id);
        }

        /// <summary>
        /// 查询设备
        /// </summary>
        /// <param name="pageModel"></param>
        /// <returns></returns>
        public MessageModel Facilities(PageModel<FacilitiesQuery> pageModel)
        {
            var exp = Expressionable.Create<FacilitieInfo>()
                .AndIF(!string.IsNullOrWhiteSpace(pageModel.Entity.Name), p => p.Name.Contains(pageModel.Entity.Name))
                .AndIF(pageModel.Entity.TypeId.HasValue, p => p.TypeId == pageModel.Entity.TypeId)
                .ToExpression();
            var page = new SqlSugar.PageModel { PageIndex = pageModel.Page.Index, PageSize = pageModel.Page.Size };
            return new MessageModel { Data = facilitieService.GetPageList(exp, page), Count = page.TotalCount };
          
        }

        /// <summary>
        /// 设备详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FacilitieInfo FacilitieDetails(long id) => facilitieService.GetById(id);

        /// <summary>
        /// 修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public void UpdateFacilitieSequence(long id, long sequence) => facilitieService.UpdateSequence(id, sequence);
        #endregion

        #region 品牌
        private void Verify(BrandInfo model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
                throw new MyException("品牌名称不能为空");
            if(brandService.Count(p=>p.Name==model.Name && p.Id!=model.Id)>0)
                throw new MyException("品牌名称已经存在");
            model.FirstLetter = Pinyin.GetPinyin(model.Name).Substring(0, 1);
        }

        /// <summary>
        /// 添加品牌
        /// </summary>
        /// <param name="model"></param>
        public void Insert(BrandInfo model)
        {
            Verify(model);
            brandService.Insert(model);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model"></param>
        public void Update(BrandInfo model)
        {
            Verify(model);
            brandService.Update(model);
        }

        /// <summary>
        /// 删除品牌
        /// </summary>
        /// <param name="id"></param>
        /// <exception cref="MyException"></exception>
        public void BrandDelete(long id)
        {
            if (venueService.Count(p => p.BrandId == id) > 0)
                throw new MyException("会议室下有该品牌不能删除");
            brandService.DeleteById(id);
        }
        /// <summary>
        /// 获取品牌列表
        /// </summary>
        /// <param name="pageModel"></param>
        /// <returns></returns>
        public MessageModel GetBrands(PageModel<BrandQuery> pageModel) => new MessageModel { Data = brandService.List(pageModel), Count = pageModel.Page.Num };
        
        /// <summary>
        /// 修改品牌排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public void UpdatetBrandSequence(long id, long sequence) => brandService.UpdateSequence(id, sequence);

        /// <summary>
        /// 修改品牌合作状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="hot"></param>
        /// <exception cref="MyException"></exception>
        public void UpdatePartner(long id, bool partner) => brandService.UpdatePartner(id, partner);

        /// <summary>
        /// 获取或者品牌
        /// </summary>
        /// <returns></returns>
        public List<BrandInfo> PartnerBrands() => brandService.GetList(p => p.IsPartner);
        #endregion
    }
}
